'use strict';


angular.module('app', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngTouch',
    'ngStorage',
    'ui.router',
    'ui.bootstrap',
    'ui.load',
    'ui.jq',
    'ui.validate',
    'oc.lazyLoad',
    'pascalprecht.translate',
    //'toastr'
]).filter("ceil",[function() {
    return function (num) { return Math.ceil(num); }
    }])
.factory('httpInterceptor', [ '$rootScope','test', '$q', '$injector',function($rootScope, test, $q, $injector) {
    var isEfficacy = true;
    var httpInterceptor = {
        'responseError' : function(response) {
            if (response.status == 401) {
                if(isEfficacy){
                    alert("登录已失效！请重新登录！");
                    location.href = "/home/Login";
                    isEfficacy = false;
                }
                return $q.reject(response);
            }else{
                if (response.status >= 400) {
                    if(response.data.Msg){
                        alert("处理失败：" + response.data.Msg);
                    }else{
                        alert("处理失败："+response.statusText);
                    }
                }
                $rootScope.myData.loadingShow = false;
            }
            return $q.reject(response);
        },
        'response' : function(response) {
            //console.log("response"+response);
            $rootScope.myData.loadingShow = false;
            return response;
        },
        'request' : function(config) {
            //console.log("request"+config);
            $rootScope.myData.loadingShow = true;
            return config;
        },
        'requestError' : function(config){
            //console.log(config);
            alert("发送数据失败："+config.statusText);
            $rootScope.myData.loadingShow = false;
            return $q.reject(config);
        }
    };
    return httpInterceptor;
}]).config([ '$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptor');
} ]).directive('loading',function(){
        return {
            restrict:'E',
            template:'<div style="justify-content:center;align-items:center;background:rgba(0 , 0 , 0 ,0.2);position: fixed;top: 0;left: 0;bottom: 0;right: 0;z-index: 1050;outline: 0;margin: 0;padding: 0;border: 0;font-size: 100%;" ng-show="myData.loadingShow"><img style="width: 100px; height: 100px;position: absolute;top: 50%;left: 50%;-webkit-transform: translate(-50%, -50%);-moz-transform: translate(-50%, -50%);-ms-transform: translate(-50%, -50%);-o-transform: translate(-50%, -50%);transform: translate(-50%, -50%);" src="/htm/img/loading.gif"/></div>'
        }
    })
    .value('test',{"loadingShow":false})
    .run (function ($rootScope) {
        $rootScope.myData = {loadingShow: true};
        $rootScope.$on('$routeChangeStart', function(event, next, current) {
        if (typeof(current) !== 'undefined'){
            $templateCache.remove(current.templateUrl);
        }
    });
    })
    .filter('DIYData' , function($filter){
        return function (input,message){
            if (message == 'yyyy-MM-dd'){
                return new Date(input).getFullYear()+"-"+(new Date(input).getMonth()+1)+"-"+new Date(input).getDate();
            }else if(message == 'MM-dd'){
                return (new Date(input).getMonth()+1)+"-"+new Date(input).getDate();
            }

            //return $filter('date')(new Date(input),message);
        }
    }).directive('myPagination',function(){
    return {
        restrict : 'ECMA',
        replace : true,    //替换的方式插入内容//绑定策略
        //scope:false,默认值，共享父作用于
        //scope:true,默认值，共享父作用于也有自己作用域
        scope : {
            pageData : '=mypagedata',
            getData : '=getPageData'
        },
        controller : ['$scope',function($scope){
            $scope.$watch('pageData', function(){
                $scope.newPageArr = [];
                for (var i=0;i<($scope.pageData.Total/$scope.pageData.Rows);i++)
                {
                    $scope.newPageArr.push({page:i+1});
                }
                //console.log($scope.newPageArr)
            });
            //watch();

        }],
        //template : '<div><button class="btn btn-sm btn-default" ng-click="getData(pageData.Page-1)" ng-disabled="pageData.Page<=1"><i class="fa fa-chevron-left text"></i><span class="text">上一页</span></button><span>第{{pageData.Page?pageData.Page:"1"}}页, 共 {{(pageData.Total/pageData.Rows)|ceil}} 页</span><button class="btn btn-sm btn-default" ng-click="getData(pageData.Page+1)" ng-disabled="pageData.Page>=(pageData.Total/pageData.Rows)"><span class="text">下一页</span><i class="fa  fa-chevron-right text"></i></button></div>'
        templateUrl:'/htm/tpl/Supplier/Paging.html'
    };
});
//.run(function($rootScope, $templateCache) {
//    $rootScope.$on('$routeChangeStart', function(event, next, current) {
//        if (typeof(current) !== 'undefined'){
//            $templateCache.remove(current.templateUrl);
//        }
//    });
//});
